PyInstaller
Install PyInstaller from PyPI:
1 | pip install pyinstaller |
Go to your program’s directory and run:
1 | pyinstaller yourprogram.py |
This will generate the bundle in a subdirectory called dist.
打包过程中可能问题
OSError: Python library not found: Python, libpython3.5m.dylib, libpython3.5.dylib, .Python

2019-02-22.15.16.57-image.png
python在安装pyenv时没有执行–enabled-shared。重新安装即可
1 | # 检查 |
成功输出

2019-02-22.15.18.11-image.png

2019-02-22.15.44.09-image.png
只生成一个可执行文件
1 | pyinstaller --onefile addup.py |

2019-02-22.15.48.54-image.png
ImportError: No module named ‘model’
1 | pyinstaller xxx.py -F --hidden-import fasttext.model |
importerror cannot import name xxx
Make sure everything is packaged correctly
看错误输出里出现了什么包没有引用,加进--hidden-import
里
python路径
os.path.realpath(file)——返回真实路径
os.path.split()——返回路径的目录和文件名
os.getcwd()——得到当前工作的目录
1 | import os |
将资源文件一起打包
示例
1 | #coding:utf-8 |
生成.spec
文件
1 | pyi-makespec -F restest.py |

2019-02-25.09.28.33-image.png
修改datas选项。
它说明需要将哪些文件加入exe,以及在零时文件夹中的命名
注意参数为tuples格式

2019-02-25.09.34.30-image.png
生成EXE文件
1 | pyinstaller -F test.spec |
总结
pyi-makespec fee_test.py --hidden-import fasttext.fasttext -F
生成.spec
文件- 在
.spec
文件中修改datas
项 pyinstaller fee_test.spec